﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using IncrementalBackups.Helpers;

namespace IncrementalBackups
{
    public class BackupFile
    {
        private const int DATE_BACKUP = 0;
        private const int FILES_FOLDER = 1;
        private const int LIST_FILES_XML = 2;
        private const int LIST_FILES_CSV = 3;
        private const int CONFIG_BACKUP = 4;
        private const int NUMBER_OF_FILES = 5;

        private string _backupDate;
        private string _destinationPath;
        private string _outputFilesXml;
        private string _outputFilesCsv;
        private string _backupConfigFile;
        private int _numberOfCopyFiles;

        public string BackupDate
        {
            get { return _backupDate; }
            set { _backupDate = value; }
        }


        public string DestinationPath
        {
            get { return _destinationPath; }
            set { _destinationPath = value; }
        }


        public string OutputFilesXml
        {
            get { return _outputFilesXml; }
            set { _outputFilesXml = value; }
        }


        public string OutputFilesCsv
        {
            get { return _outputFilesCsv; }
            set { _outputFilesCsv = value; }
        }


        public string BackupConfigFile
        {
            get { return _backupConfigFile; }
            set { _backupConfigFile = value; }
        }


        public int NumberOfCopyFiles
        {
            get { return _numberOfCopyFiles; }
            set { _numberOfCopyFiles = value; }
        }

        public BackupFile()
        { }
        public BackupFile(string backupDate, string destinationPath, string outputFilesXml,
            string outputFilesCsv, string backupConfigFile, int numberOfCopyFiles)
        {
        }

        public BackupFile(string fileLocation)
        {
        }

        public void WriteBackupFileToDisk(string targetFileName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(DateTime.Now.ToString());
            sb.AppendLine(DestinationPath);
            sb.AppendLine(OutputFilesXml);
            sb.AppendLine(OutputFilesCsv);
            sb.AppendLine(BackupConfigFile);
            sb.AppendLine(NumberOfCopyFiles.ToString());
            File.WriteAllText(targetFileName, sb.ToString());
        }
        public bool LoadRestoreFile(string fileLocation)
        {
            bool result = true;
            try
            {
                using (StreamReader sr = new StreamReader(fileLocation))
                {
                    int lineIndex;
                    string line;
                    int numberOfFileInFolder = -1;
                    int numberOfFilesInConfig = -2;
                    int numberOfFilesInShortCut = -3;
                    for (lineIndex = 0; (line = sr.ReadLine()) != null; lineIndex++)
                    {
                        switch (lineIndex)
                        {
                            case DATE_BACKUP:
                                DateTime backupDate;
                                if (!DateTime.TryParse(line, out backupDate))
                                    throw new Exception("Error in back date");
                                else
                                    BackupDate = backupDate.ToString();
                                break;
                            case FILES_FOLDER:
                                if (Directory.Exists(line))
                                    numberOfFileInFolder = Directory.GetFiles(line).Length;
                                else
                                    throw new Exception("Back up files directory doesn't exist");
                                break;
                            case LIST_FILES_XML:
                                if (File.Exists(line))
                                {
                                    try
                                    {
                                        FilesDS.FileListDataTable dt = new FilesDS.FileListDataTable();
                                        dt.ReadXml(line);
                                        numberOfFilesInConfig = dt.Count;
                                        OutputFilesXml = line;
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Error read backup files list");
                                    }
                                }
                                else
                                    throw new Exception("Backup files list doesn't exist");
                                break;
                            case LIST_FILES_CSV:
                                if (File.Exists(line))
                                {
                                    try
                                    {
                                        OutputFilesCsv = line;
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Error read backup files list csv");
                                    }
                                }
                                else
                                    throw new Exception("Backup files list csv doesn't exist");
                                break;
                            case CONFIG_BACKUP:
                                if (File.Exists(line))
                                {
                                    try
                                    {
                                        BackupConfiguration bc = new BackupConfiguration();
                                        bc.ReadXml(line);
                                        BackupConfigFile = line;
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Error read configuration file");
                                    }
                                }
                                else
                                    throw new Exception("configuration file doesn't exist");
                                break;
                            case NUMBER_OF_FILES:
                                if (!int.TryParse(line, out numberOfFilesInShortCut))
                                    throw new Exception("Error read number of lines");
                                break;

                            default:
                                break;
                        }

                    }
                    if (numberOfFileInFolder == numberOfFilesInConfig && numberOfFilesInConfig == numberOfFilesInShortCut)
                    {
                        NumberOfCopyFiles = numberOfFilesInConfig;

                    }
                    else
                        throw new Exception("Number of backup files didn't macth");

                }
            }
            catch (Exception ex)
            {
                LogsHelper.WriteToErrorLog(ex, "Error load config file from shortcut", false);
                result = false;
            }
            return result;
        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}
